<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import userState, { getFollowing } from '../store.js'

const router = useRouter()
const isLoggedIn = computed(() => userState.isLoggedIn)
const followingUsers = computed(() => getFollowing())

// 横向滚动容器引用
const scrollContainer = ref(null)

const goToLogin = () => {
  router.push('/login')
}

// 查看用户主页
const viewUserProfile = (userId) => {
  router.push(`/user/${userId}`)
}

// 自动加载更多数据
const loadMore = () => {
  // 实际项目中这里应该调用API加载更多数据
  console.log('加载更多关注用户')
}

// 监听滚动事件
const handleScroll = () => {
  if (!scrollContainer.value) return
  
  const { scrollLeft, clientWidth, scrollWidth } = scrollContainer.value
  
  // 当滚动到距离右侧100px时加载更多
  if (scrollWidth - scrollLeft - clientWidth < 100) {
    loadMore()
  }
}

onMounted(() => {
  if (scrollContainer.value) {
    scrollContainer.value.addEventListener('scroll', handleScroll)
  }
})

onUnmounted(() => {
  if (scrollContainer.value) {
    scrollContainer.value.removeEventListener('scroll', handleScroll)
  }
})
</script>

<template>
  <div class="follow-container">
    <div class="page-header">
      <h1>我的关注</h1>
    </div>
    
    <div v-if="!isLoggedIn" class="not-login">
      <div class="login-prompt">
        <h3>请先登录</h3>
        <p>登录后才能查看关注的用户</p>
        <button @click="goToLogin" class="login-btn">去登录</button>
      </div>
    </div>
    
    <div v-else class="follow-content">
      <div v-if="followingUsers.length === 0" class="empty-state">
        <div class="empty-icon">
          <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="#666" stroke-width="2">
            <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
            <circle cx="9" cy="7" r="4"></circle>
            <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
            <path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
          </svg>
        </div>
        <h3>还没有关注任何用户</h3>
        <p>去发现页面关注感兴趣的用户吧</p>
        <RouterLink to="/" class="explore-btn">去发现</RouterLink>
      </div>
      <div v-else class="horizontal-scroll-container" ref="scrollContainer">
        <div class="horizontal-scroll-content">
          <div v-for="user in followingUsers" :key="user.id" class="user-card horizontal-card" @click="viewUserProfile(user.id)">
            <div class="user-avatar">
              <img :src="user.avatar" alt="用户头像" class="avatar">
            </div>
            <div class="user-info">
              <h4 class="username">{{ user.username }}</h4>
              <p class="user-bio">{{ user.bio }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.follow-container {
  min-height: 100vh;
  background-color: #ffffff;
  color: #333333;
  padding: 20px;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.page-header h1 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.follow-content {
  width: 100%;
  min-height: 60vh;
  padding: 20px;
}

/* 横向滚动容器 */
.horizontal-scroll-container {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: #ddd #f5f5f5;
}

/* 滚动条样式 */
.horizontal-scroll-container::-webkit-scrollbar {
  height: 6px;
}

.horizontal-scroll-container::-webkit-scrollbar-track {
  background: #f5f5f5;
  border-radius: 3px;
}

.horizontal-scroll-container::-webkit-scrollbar-thumb {
  background: #ddd;
  border-radius: 3px;
}

.horizontal-scroll-container::-webkit-scrollbar-thumb:hover {
  background: #bbb;
}

/* 横向内容容器 */
.horizontal-scroll-content {
  display: flex;
  padding: 20px;
  gap: 20px;
  min-width: max-content;
}

/* 横向卡片样式 */
.horizontal-card {
  width: 280px;
  flex-shrink: 0;
  margin-bottom: 0;
}

/* 用户卡片样式 */
.user-card {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #f0f0f0;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.user-avatar {
  flex-shrink: 0;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #f0f0f0;
}

.user-info {
  flex: 1;
  min-width: 0;
}

.username {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 6px;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-bio {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  margin-bottom: 20px;
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 20px;
  margin-bottom: 10px;
  color: #333;
}

.empty-state p {
  color: #666;
  margin-bottom: 30px;
  font-size: 16px;
}

.explore-btn {
  display: inline-block;
  padding: 12px 30px;
  background-color: #ff2442;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.explore-btn:hover {
  background-color: #e01e38;
}

.not-login {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
}

.login-prompt {
  text-align: center;
  background-color: #ffffff;
  border-radius: 12px;
  padding: 40px;
  border: 1px solid #f0f0f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.login-prompt h3 {
  font-size: 24px;
  margin-bottom: 10px;
  color: #333;
}

.login-prompt p {
  color: #666;
  margin-bottom: 30px;
  font-size: 16px;
}

.login-btn {
  display: inline-block;
  padding: 12px 30px;
  background-color: #ff2442;
  color: white;
  text-decoration: none;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  transition: background-color 0.3s;
  cursor: pointer;
}

.login-btn:hover {
  background-color: #e01e38;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header h1 {
    font-size: 24px;
  }
  
  .empty-state,
  .not-login {
    padding: 40px 15px;
  }
  
  .login-prompt {
    padding: 30px 20px;
  }
  
  .horizontal-card {
    width: 240px;
  }
  
  .horizontal-scroll-content {
    padding: 16px;
    gap: 16px;
  }
  
  .user-card {
    padding: 16px;
    gap: 12px;
  }
  
  .avatar {
    width: 56px;
    height: 56px;
  }
  
  .username {
    font-size: 16px;
  }
  
  .user-bio {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .horizontal-card {
    width: 200px;
  }
  
  .horizontal-scroll-content {
    padding: 12px;
    gap: 12px;
  }
  
  .user-card {
    padding: 12px;
    gap: 10px;
  }
  
  .avatar {
    width: 48px;
    height: 48px;
  }
  
  .username {
    font-size: 15px;
  }
}
</style>